<template>
    <div class="app-container">
        <div class="customContainer ">
            <!-- 搜索头 -->
            <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
                <el-form-item>
                    <el-date-picker :editable="false" :clearable="false" v-model="pickerDate" value-format="yyyy.MM.dd"
                        type="daterange" align="right" unlink-panels range-separator="-" start-placeholder="开始日期"
                        end-placeholder="结束日期" :picker-options="pickerOptions" @change="dateChange"></el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-select filterable placeholder="选择门店" v-model="formInline.store_id" @change="storeChange" style="width:160px">
                        <el-option v-for="item in storeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-select clearable placeholder="授权员工" v-model="formInline.emp_id" style="width:160px" @change="searchClick">
                        <el-option v-for="item in clerkList" :key="item.emp_id" :label="item.nick" :value="item.emp_id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <dj-button size="small" @search="searchClick" />
                </el-form-item>
            </el-form>
            <!-- 权限tap标签分类 -->
            <el-tabs v-model="formInline.type" @tab-click="tabClick">
                <el-tab-pane label="赔币查询" name="1"></el-tab-pane>
                <el-tab-pane label="赔票查询" name="2"></el-tab-pane>
            </el-tabs>
            <!-- 表格 -->
            <transition name="fade" mode="out-in">
                <el-table  highlight-current-row show-summary :key="formInline.type" :data="list" size="small" v-loading.body="listLoading"
                    border :summary-method="getSummaries">
                    <el-table-column align="center" prop="type_name" :label="formInline.type==0?'赔币类型':'赔票类型'"></el-table-column>
                    <el-table-column align="center" prop="num" label="数量"> </el-table-column>
                    <el-table-column align="center" prop="date_create" label="日期"></el-table-column>
                    <el-table-column align="center" prop="card_no" label="会员卡号"></el-table-column>
                    <el-table-column align="center" prop="nick" label="授权员工"></el-table-column>
                    <el-table-column align="center" prop="remark" label="备注"></el-table-column>
                </el-table>
            </transition>
        </div>
    </div>
</template>
<script>
    import {
        getList,
        getPage,
        getClerkList,
        getStoreList,
    } from "@/api/member/auth_query";
    import { pickerOptionsF, pickerDateF, optionMaxTime } from '@/utils';
    export default {
        data() {
            return {
                list: [],
                listLoading: false,
                storeList: [],
                clerkList: [],
                formInline: {
                    store_id: '',
                    begin_time: '',
                    type: '1',
                    end_time: '',
                    emp_id: ''
                },
                pickerOptions: pickerOptionsF(),
                pickerDate: pickerDateF()
            }
        },
        created() {
            this.getStore()
        },
        methods: {
            tabClick() {
                this.searchClick()
            },
            getSummaries(param) {
                const { columns, data } = param;
                const sums = [];
                columns.forEach((column, index) => {
                    const values = data.map(item => Number(item[column.property]));
                    if (index === 0) {
                        sums[index] = '合计';
                        return;
                    }
                    if (index == 1) {
                        if (!values.every(value => isNaN(value))) {
                            sums[index] = values.reduce((prev, curr) => {
                                const value = Number(curr);
                                if (!isNaN(value)) {
                                    return prev + curr;
                                } else {
                                    return prev;
                                }
                            }, 0);
                            sums[index] = (sums[index] * 1).toFixed(2) * 1;
                        }
                    } else {
                        sums[index] = '-';
                    }
                });
                return sums;
            },
            dateChange() {
                this.storeChange();
            },
            getStore() {
                getStoreList({ category_id: this.$route.meta.id }).then(response => {
                    let data = response.filter(res => 
                        res.id == this.$store.getters.optionId
                    ) 
                    if(data.length>0){
                        this.formInline.store_id = data[0].id;
                    } else { 
                        if(response.length > 0) {
                            this.formInline.store_id = response[0].id;
                        }
                    }
                    this.storeList = response;
                    this.storeChange();
                });
            },
            storeChange() {
                this.formInline.emp_id = '';
                this.formInline.begin_time = this.pickerDate ? this.pickerDate[0] : '';
                this.formInline.end_time = this.pickerDate ? this.pickerDate[1] : '';
                getClerkList(this.formInline).then(response => {
                    this.clerkList = response;
                });
            },
            // 搜索
            searchClick() {
                optionMaxTime(this.pickerDate).then(() => {//最大可选择时间区间
                    this.formInline.begin_time = this.pickerDate ? this.pickerDate[0] : '';
                    this.formInline.end_time = this.pickerDate ? this.pickerDate[1] : '';
                    this.fetchData();
                });
            },
            // 列表获取
            fetchData() {
                this.listLoading = true;
                getList(this.formInline).then(response => {
                    response.forEach(val => {
                        switch (val.reason) {
                            case '0':
                                val.type_name = '无'
                                break;
                            case '1':
                                val.type_name = this.formInline.type == 1 ? '维修用币' : '存票机故障';
                                break;
                            case '2':
                                val.type_name = '公关用币'
                                break;
                            case '3':
                                val.type_name = '巡场用币'
                                break;
                            case '4':
                                val.type_name = '企划用币'
                                break;
                            case '5':
                                val.type_name = '清币和转币'
                                break;
                            default:
                                val.type_name = '-'
                                break;
                        }
                    });
                    this.list = response;
                    this.listLoading = false;
                }).catch(res=>{
                    this.listLoading=false;
                })
            },
        }
    }
</script>